package com.pactera.madp.cp.service.sync.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.cp.api.dto.ContactRadarDto;
import com.pactera.madp.cp.api.entity.RadarContactEntity;
import com.pactera.madp.cp.api.entity.WorkContactEmployeeEntity;
import com.pactera.madp.cp.api.entity.sync.MaRadarEntity;
import com.pactera.madp.cp.api.vo.ContactRadarVO;
import com.pactera.madp.cp.mapper.sync.MaRadarMapper;
import com.pactera.madp.cp.service.IWorkContactEmployeeService;
import com.pactera.madp.cp.service.IWorkEmployeeService;
import com.pactera.madp.cp.service.RadarContactService;
import com.pactera.madp.cp.service.sync.MaRadarService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @title 经理视图 -> 客户雷达表 业务接口实现
 * @date 2022-05-14
 * @author Deming.Chang
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MaRadarServiceImpl extends ServiceImpl<MaRadarMapper, MaRadarEntity> implements MaRadarService {

    private final RadarContactService radarContactService;
    private final IWorkEmployeeService employeeService;
    private final IWorkContactEmployeeService contactEmployeeService;

    @Override
    public int handlerAndSave(Long lastMaxReceiveTime) {
        return baseMapper.handlerAndInsert(lastMaxReceiveTime);
    }

    @Override
    public int removeHisData(String maDate, int offset) {
        String date = DateUtil.format(DateUtil.offsetDay(DateUtil.parse(maDate), offset), DatePattern.NORM_DATE_PATTERN);
        return baseMapper.delete(Wrappers.<MaRadarEntity>query().lambda().le(MaRadarEntity::getDate, date));
    }

    @Override
    public List<ContactRadarVO> top3(String wxUserId) {
        Integer employeeId = employeeService.getEmpIdByWxUserId(wxUserId);
        List<Integer> contactIds = contactEmployeeService.list(
                Wrappers.<WorkContactEmployeeEntity>lambdaQuery()
                        .eq(WorkContactEmployeeEntity::getEmployeeId, employeeId)
                        .eq(WorkContactEmployeeEntity::getDelFlag, 0)
        ).stream().map(WorkContactEmployeeEntity::getContactId).collect(Collectors.toList());
        List<ContactRadarVO> list = this.baseMapper.top3(wxUserId);
        list.stream().forEach(vo -> {
            handItem(vo, contactIds);
        });
        return list;
    }

    @Override
    public IPage<ContactRadarVO> pageResult(Page page, ContactRadarDto dto) {
        Integer employeeId = employeeService.getEmpIdByWxUserId(dto.getWxUserId());
        List<Integer> contactIds = contactEmployeeService.list(
                Wrappers.<WorkContactEmployeeEntity>lambdaQuery()
                        .eq(WorkContactEmployeeEntity::getEmployeeId, employeeId)
                        .eq(WorkContactEmployeeEntity::getDelFlag, 0)
        ).stream().map(WorkContactEmployeeEntity::getContactId).collect(Collectors.toList());
        IPage<ContactRadarVO> pageResult = this.baseMapper.pageResult(page, dto);
        pageResult.getRecords().stream().forEach(vo -> {
            handItem(vo, contactIds);
        });
        return pageResult;
    }

    private void handItem(ContactRadarVO vo, List<Integer> contactIds) {
        String distinctId = vo.getDistinctId();
        Integer size = distinctId.length();
        // 10位为核心客户号
        if (size == 10) {
            RadarContactEntity entity = radarContactService.getOne(
                    Wrappers.<RadarContactEntity>lambdaQuery()
                            .eq(RadarContactEntity::getCustNo, distinctId), false
            );
            if (entity != null) {
                vo.setHeadimgurl(entity.getHeadimgurl());
                vo.setNickname(entity.getNickname());
                vo.setType(entity.getType());
                vo.setWxExternalUserid(entity.getWxExternalUserid());
                vo.setMobileTel(entity.getMobileTel());
                vo.setContactId(entity.getContactId());
            }
        }
        Integer contactId = vo.getContactId();
        if (contactIds.contains(contactId)) {
            vo.setIsContact(true);
        } else {
            vo.setIsContact(false);
        }
    }

}

